package bavaria.hightec.banking;

import java.util.Calendar;
import java.util.Date;

import bavaria.hightec.conditions.GiroAccountConditions;
import bavaria.hightec.conditions.RateOfInterest;
import bavaria.hightec.exceptions.OverCheckException;

public class GiroAccount extends Account {
	
	
	private long maxvalue;
	RateOfInterest roi;
	
	
	public GiroAccount(Date creationDate,GiroAccountConditions conditions,RateOfInterest roi,int accountNumber,String accountHolder){
		super(creationDate,accountNumber,accountHolder);
		this.maxvalue=conditions.getCreditHistory();
		this.roi=roi;
		
	}
	
	public void debitEntry(Money amount,String matter) throws OverCheckException{
		if((amount.getAmount()+getBalance())<maxvalue){
			throw new OverCheckException("Girokonto nicht weiter ueberziehbar. Ueberziehungsrahmen ausgesch�pft.");}
		else
		super.debitEntry(amount, matter);
		
	}
	
	public void chargeInterest() throws OverCheckException{
		if(BankImpl.c2.get(Calendar.DAY_OF_MONTH) == 1){
			long balance=getBalance();
			if(balance>=0){
				balance=(long) (balance*(this.roi.getCreditInterest()/100));
				Money money=new Money(balance,getCurrency());
				creditEntry(money, "Verzinsen");
			}
			else if((balance<0)){
				balance=(long) -(balance*(this.roi.getDebitInterest()/100));
				Money money=new Money(balance,getCurrency());
				debitEntry(money,"Verzinsen");
			}
		}
	}
	
	public String toString(){
		String string="Kontoart: Giro "+ super.toString()+ " Habzins: "+roi.getCreditInterest()+" Sollzins: " +roi.getDebitInterest();
		return string;
	}

}
